package edu.mis.core.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Record;

/**
 * 菜单easyUI Tree工具类<br>
 * 在构建菜单树时需要使用。IN AccountCaption Controller
 */
public class TreeUtil1 {
	public static List<Map<String,Object>> findTree(List<Record> list) {
		List<Map<String,Object>> captions = new ArrayList<Map<String,Object>>();
		for (int i = 0; i < list.size(); i++) {
			Record p = list.get(i);
			if (p.get("parentId") == null || p.getInt("parentId") == 0) {
				List<Map<String,Object>> children = getChildren(list, p.get("captionId"));
				Map<String,Object> map=new HashMap<String,Object>();
				map.put("id", p.get("id"));
				map.put("text", p.get("name"));
				map.put("name", p.get("name"));
				map.put("captionId", p.get("captionId"));
				map.put("category", p.get("category"));
				map.put("balanceDirection", p.get("balanceDirection"));
				map.put("status", p.get("status"));
				map.put("isUse", p.get("isUse"));
				if (children != null && children.size() > 0){
					map.put("children", children);
					map.put("state", "closed");
				}
				captions.add(map);
			}
		}
		return captions;
	}

	public static List<Map<String,Object>> getChildren(List<Record> list, Object parentId) {
		List<Map<String,Object>> children = new ArrayList<Map<String,Object>>();
		for (int i = 0; i < list.size(); i++) {
			Record p = list.get(i);
			if (parentId.equals(p.get("parentId"))) {
				Map<String,Object> map=new HashMap<String,Object>();
				map.put("id", p.get("id"));
				map.put("text", p.get("name"));
				map.put("name", p.get("name"));
				map.put("captionId", p.get("captionId"));
				map.put("category", p.get("category"));
				map.put("balanceDirection", p.get("balanceDirection"));
				map.put("status", p.get("status"));
				map.put("parentId", p.get("parentId"));
				map.put("isUse", p.get("isUse"));
				List<Map<String,Object>> c = getChildren(list, p.get("captionId"));
				if (c != null && c.size() > 0){
					map.put("children", c);
					map.put("state", "closed");
				}
				children.add(map);
			}
		}
		return children;
	}
}
